package com.xiaogang.ggfw.service;

import com.xiaogang.ggfw.config.error.GgfwException;
import com.xiaogang.ggfw.model.dealofd.DealOfdParam;
import com.xiaogang.ggfw.utils.convert.OfdConvertUtil;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.List;

/**
 * @ProjectName : ggfw
 * @作者 : 侯小刚
 * @描述 :
 * @创建日期 : 2023/8/9 15:32
 */
@Service
public class DealOfdService {

    private Logger logger = org.slf4j.LoggerFactory.getLogger(DealOfdService.class);

    public List<String> ofdToImg(DealOfdParam param) {
        if(param == null) {
            throw new GgfwException("参数不能为空");
        }
        if(!StringUtils.hasLength(param.getBase64Ofd())) {
            throw new GgfwException("ofd文件base64不能为空");
        }
        // 将base64转换为InputStream
        try {
            InputStream inputStream = OfdConvertUtil.base64ToInputStream(param.getBase64Ofd());
            return OfdConvertUtil.ofdToImg(inputStream);
        }catch (Exception e) {
            this.logger.error("ofd文件转换img失败",e);
            throw new RuntimeException("ofd文件转换img失败");
        }
    }

    public Object ofdToPdf(DealOfdParam param) {
        if(param == null) {
            throw new GgfwException("参数不能为空");
        }
        if(!StringUtils.hasLength(param.getBase64Ofd())) {
            throw new GgfwException("ofd文件base64不能为空");
        }
        // 将base64转换为InputStream
        try {
            InputStream inputStream = OfdConvertUtil.base64ToInputStream(param.getBase64Ofd());
            return OfdConvertUtil.ofdToPdf(inputStream);
        }catch (Exception e) {
            this.logger.error("ofd文件转换pdf失败",e);
            throw new RuntimeException("ofd文件转换pdf失败");
        }
    }
}
